package com.springMVC.controller;

import com.springMVC.service.OrderService;
import com.springMVC.util.AppHandler;
import com.springMVC.util.Rest;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能模块说明
 * Created with IntelliJ IDEA.
 * User: AnCong
 * Date: 2017/4/27
 */
@Controller
public class OrderController{

    private static ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/spring.xml"
            , "classpath:conf/spring-mybatis.xml"});
    private static OrderService orderService
            =(OrderService) context.getBean("orderServiceImpl");

    @ResponseBody
    @RequestMapping("/orderList")
    public Rest orderList(){
        Map<String,String> params= AppHandler.params();
        Rest rest=new Rest();
        try{
            rest=orderService.orderList(params);
        }catch(Exception e){
            rest.setIsSuccess(false);
            rest.setError("查询列表失败");
            e.printStackTrace();
        }
        return rest;
    }
    @RequestMapping("/upLoadPicture")
    public String upLoadPicture(MultipartFile file) throws IOException{
        ApiRest apiRest = new ApiRest();
        Map<String,String> params= AppHandler.params();
        if (file==null) {
            apiRest.setIsSuccess(false);
            apiRest.setError("文件上传错误！");
            return BeanUtils.toJsonStr(apiRest);
        }
        if (params.get("relativePath")==null||params.get("relativePath").equals("")) {
            apiRest.setIsSuccess(false);
            apiRest.setError("路径参数错误！");
            return BeanUtils.toJsonStr(apiRest);
        }
        String imageType = file.getContentType();
        if (imageType.equals("image/jpg") ||imageType.equals("image/jpeg") || imageType.equals("image/gif") || imageType.equals("image/png") || imageType.equals("image/bmp")) {
            int i = 1;
            long l = file.getSize();
            if (l < i * 1024 * 1024) {
                String imageFileName = Common.getNowCorrect2Millisecond() + "" ;//+imageFile.getOriginalFilename();
                String path = PropertyUtils.getDefault("picture_share_directory");
                String filePath = path + URLDecoder.decode(params.get("relativePath"), "UTF-8");
                File targetImagePathFile = new File(filePath);
                if (!targetImagePathFile.exists()) {
                    targetImagePathFile.mkdirs();
                }
                //创建文件
                File ImageFile = new File(filePath + "/" + imageFileName + "." + imageType.split("/")[1]);
                file.transferTo(ImageFile);
                Map map = new HashMap();
                map.put("dbpath", params.get("relativePath") + "/" + imageFileName + "." + imageType.split("/")[1]);
                apiRest.setIsSuccess(true);
                apiRest.setMessage("上传图片成功！");
                apiRest.setData(map);
            } else {
                apiRest.setIsSuccess(false);
                apiRest.setError("上传图片大小不得超过1M！");
            }
        } else {
            apiRest.setIsSuccess(false);
            apiRest.setError("上传图片只能为jpg、gif、png、bmp格式！");
        }
        return BeanUtils.toJsonStr(apiRest);
    }
}
